<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title>Title</title>
    <script src="./js/vue.js"></script>
    <script src="./js/vue-router.js"></script>
</head>
<body>
    <div id="app">
        <h1>{{msg}}</h1>
        <router-link to="/home">home</router-link>
        <router-link to="/list">list</router-link>
        <router-link :to="{name:'user',params:{userId:123}}">user</router-link>
        <router-view></router-view>
    </div>
    <script>
        let home = {
            template: `
              <div>
              <div>home-----{{ $route.query.name }}</div>
              <button @click="goList">点击跳转到list页面</button>
              </div>`,
            methods: {
                goList() {
                    // this.$router.push("/list");
                    this.$router.push({
                        name: "list", params: {
                            id: 100
                        }
                    })
                }
            }
        }
        let list = {
            template: `
              <div>
              <div>list----{{ $route.params.id }}</div>
              <button @click="goBack">点击回退到home页面</button>
              </div>`,
            methods: {
                goBack() {
                    // this.$router.go(-1);
                    this.$router.push({
                        path: "/home", query: {name: "zs"}
                    });
                }
            }
        }
        let user = {
            template: `<div>
                    <div>user----{{$route.params.userId}}</div>
                </div>`
        }
        let router = new VueRouter({
            // mode:"history",
            routes: [
                {
                    name: "user",
                    path: "/user/:userId",
                    component: user
                },
                {
                    name: "home",
                    path: "/home",
                    component: home
                },
                {
                    name: "list",
                    path: "/list/:id",
                    component: list
                }
            ]
        })
        let vm = new Vue({
            el: "#app",
            data: {
                msg: "vue"
            },
            methods: {},
            router
        });
    </script>
</body>
</html>